package com.javakc.mes.annual.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javakc.commonutils.api.R;
import com.javakc.mes.annual.entity.Annual;
import com.javakc.mes.annual.service.AnnualService;
import com.javakc.servicebase.config.InstructionQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="年度计划表")
@RestController
@RequestMapping("/mes/annual")
public class AnnualController {

    @Autowired
    private AnnualService annualService;

    @GetMapping
    public R finAll(){
        List<Annual> list = annualService.list();
        return R.OK().data("items",list);
    }

    @ApiOperation(value = "根据条件进行分页查询 年度计划")
    @PostMapping("{pageNo}/{pageSize}")
    public R findPageAnnual(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Long pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Long pageSize,
            @ApiParam(name = "instructionQuery", value = "查询条件封装对象", required = false)
            @RequestBody(required = false) InstructionQuery instructionQuery) {
        Page<Annual> annualPage  = new Page<>(pageNo, pageSize);
        annualService.findPageAnnual(annualPage,instructionQuery);
        long total = annualPage.getTotal();
        List<Annual> annualList = annualPage.getRecords();
        return R.OK().data("total", total).data("items", annualList);
    }

    @ApiOperation(value = "新增年度指令")
    @PostMapping("saveAnnual")
    public R saveAnnual(@RequestBody Annual annual){
        boolean flag = annualService.save(annual);
        if(flag)
            return R.OK();
        else
            return R.ERROR();
    }

    @ApiOperation(value = "根据Id查询单条数据")
    @GetMapping("getAnnualByid/{annualId}")
    public R getAnnualById(@PathVariable("annualId") int annualId){
        Annual annual = annualService.getById(annualId);
        return R.OK().data("items",annual);
    }


    @ApiOperation("修改年度计划")
    @PutMapping("updateAnnual")
    public R updateAnnual(@RequestBody Annual annual){
        boolean flag = annualService.updateById(annual);
        if(flag)
            return R.OK();
        else
            return  R.ERROR();
    }

    @ApiOperation("根据Id删除年度计划")
    @DeleteMapping("deleteAnnualById/{annualId}")
    public R deleteAnnual(@PathVariable("annualId") String annualId){
        boolean flag = annualService.removeById(annualId);
        if (flag)
            return R.OK();
        else
            return R.ERROR();
    }


}
